const connection = require("../app/database");

class LabelService {
	// 创建标签
	async create(name) {
		// 将用户存储到数据库
		const statement = `INSERT INTO label (name) VALUES(?);`;
		const [result] = await connection.execute(statement, [name]);

		// 返回数据
		return result;
	}

	// 获取标签数据
	async getLabelByName(name) {
		// 查找数据库
		const statement = `SELECT * FROM label WHERE name = ?;`;
		const [result] = await connection.execute(statement, [name]);
		return result[0];
	}

	// 获取标签列表
	async getLabelList(offset = 0, size = 10) {
		// 查找数据库
		const statement = `SELECT * FROM label LIMIT ?, ?;`;
		const [result] = await connection.execute(statement, [offset, size]);
		return result;
	}

	// 修改标签
	async update(name, id) {
		const statement = `UPDATE label SET name = ? WHERE id =?;`;
		const [result] = await connection.execute(statement, [name, id]);

		// 返回数据
		return result;
	}

	// 修改标签
	async remove(id) {
		const statement = `DELETE FROM label WHERE id =?;`;
		const [result] = await connection.execute(statement, [id]);

		// 返回数据
		return result;
	}
}

module.exports = new LabelService();
